<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link href="../css/aui.css" rel="stylesheet" type="text/css"/>
    <link href="../css/aui-iconfont.css" rel="stylesheet"/>
    <link href="../css/index.css" rel="stylesheet" type="text/css"/>
    <style type="text/css">
        textarea{
            height: 300px;
        }
        body{
            background: #ffffff;
        }
        .aui-form input{
            width: calc(100% - 160px)!important;
        }
        .aui-form textarea{
            width: calc(100% - 100px)!important;
            height: 60px!important;
        }
        .aui-pull-right{
            text-align: right;
        }
        #searchval{
            border: 1px #eeeeee solid;border-radius: 6px;width: 200px!important;padding:4px;height: 28px;
        }
		#tab2_context{
			width: 100%;
			height: calc(100% - 140px);
		}
    </style>
</head>
<body class="scrollbar">
<div class="layout" style="height: 50px" id="tab2_tools">
    <button class="aui-btn aui-btn-primary" onclick="bySwagger()">从swagger获取</button>
    <button class="aui-btn aui-btn-info" onclick="byFindler();">从 findler 获取</button>
    <button onclick="generateCodeAll();">生成全部代码</button>
    <button class="aui-btn aui-btn-default" id="hasDelBtn" onclick="hasDelList();">已屏蔽列表</button>
    <button class="aui-btn aui-btn-info" onclick="rootUrlManager();">ROOT URL管理</button>
    <button class="aui-btn aui-btn-info" onclick="currentUse();">当前使用项目</button>
    <button class="aui-btn aui-btn-info" onclick="openSourcePath();">打开生成目录</button>
</div>
<div class="layout" style="height: 50px" id="tab2_tools2">
    <div class="aui-col-xs-12">
        <div class="aui-col-xs-10">
            <input type="checkbox" class="aui-input aui-input-select"/>全选
            <button class="aui-btn aui-btn-primary" onclick="generateCodeBySelect('')">生成</button>
            <button class="aui-btn aui-btn-warning" onclick="delApiBySelect('')">屏蔽</button>
            <button class="aui-btn" id="selectSource" onclick="showSelectSource();">数据源:全部</button>
            <input id="searchval"  placeholder="请输入搜索内容"/> <button onclick="searchList()">搜索</button>
        </div>
        <div class="aui-col-xs-2 aui-pull-right">
            <button class="aui-btn aui-btn-success" onclick="addActions();">添加</button>
        </div>
    </div>
</div>
<div id="tab2_context" class="scrollbar"></div>
<script src="../js/template.js" type="text/javascript"></script>
<script src="../js/api.js" type="text/javascript"></script>
<!--API列表--->
<script id="apiDom" type="text/html">
    {{ for(var a in it) { }}
        <div class="layout" id="api_layout_{{=a}}">
            <div class="aui-col-xs-12">
                <div class="aui-col-xs-8">
                    <div class="input aui-ellipsis-1">
                    <input type="checkbox" class="aui-input aui-input-select"/>{{=it[a].name}}
                    </div>
                    </div>
                <div class="aui-col-xs-4  aui-pull-right">
                    <div class="aui-btn aui-btn-primary" onclick="generateCodeByGroupId('{{=it[a].id}}')">生成</div>
                    {{? it[a].del }}
                    <div class="aui-btn aui-btn-danger" onclick="delApiByGroupId('{{=a}}','{{=it[a].id}}')">删除</div>
                    {{??}}
                    <div class="aui-btn aui-btn-warning" onclick="delApiByGroupId('{{=a}}','{{=it[a].id}}')">屏蔽</div>
                    {{?}}
                    <div class="aui-btn aui-btn-success"  onclick="toggleLayout(this,'{{=a}}')">展开</div>
                </div>
            </div>
            <div style="width: 100%;height: 25px;"></div>
            <div id="api_dom_{{=a}}" class="hide">
            {{ for(var i in it[a].apiActions) { }}
                <div  class="api_dom"  id="api_dom_{{=a}}_{{=i}}">
                    <div class="api_url">{{=it[a].apiActions[i].url || ''}}</div>
                    <div class="aui-col-xs-12">
                        别名<input type="text" id="api_alias_{{=a}}_{{=i}}" value="{{=it[a].apiActions[i].alias || ''}}"/>
                        请求方式: {{=it[a].apiActions[i].method || ''}}
                        <input type="button" class="aui-btn" value="修改" onclick="changeAlias('api_alias_{{=a}}_{{=i}}','{{=it[a].apiActions[i].id}}');"/>
                    </div>
                    <div class="btn-group">
                        {{? it[a].apiActions[i].del }}
                            <div class="aui-btn aui-btn-success" onclick="viewApi('{{=a}}','{{=i}}')">查看</div>
                            <div class="aui-btn aui-btn-primary" onclick="generateCode('{{=it[a].apiActions[i].id}}')">生成管理端</div>
                            <div class="aui-btn aui-btn-primary" onclick="generateCode1('{{=it[a].apiActions[i].id}}')">生成用户端</div>
                            <div class="aui-btn aui-btn-warning" onclick="delApi('{{=a}}','{{=i}}','{{=it[a].apiActions[i].id}}')">取消屏蔽</div>
                            <div class="aui-btn aui-btn-danger" onclick="delApi('{{=a}}','{{=i}}','{{=it[a].apiActions[i].id}}')">删除</div>
                        {{??}}
                            <div class="aui-btn aui-btn-success" onclick="viewApi('{{=a}}','{{=i}}')">查看</div>
                            <div class="aui-btn aui-btn-info" onclick="remark('{{=a}}','{{=i}}')">备注</div>
                            <div class="aui-btn aui-btn-primary" onclick="generateCode('{{=it[a].apiActions[i].id}}')">生成管理端</div>
                            <div class="aui-btn aui-btn-primary" onclick="generateCode1('{{=it[a].apiActions[i].id}}')">生成用户端</div>
                            <div class="aui-btn aui-btn-primary" onclick="generateCode2('{{=it[a].apiActions[i].id}}')">生成PC端</div>
                            <div class="aui-btn aui-btn-warning" onclick="delApi('{{=a}}','{{=i}}','{{=it[a].apiActions[i].id}}')">屏蔽</div>
                            <div class="aui-btn aui-btn-success" onclick="runApi('{{=a}}','{{=i}}')">执行</div>
                        {{?}}
                    </div>
                    <div id="tips_{{=it[a].apiActions[i].id}}"></div>
                </div>
            {{ } }}
            </div>
        </div>
    {{ } }}
</script>
<!--swagger api 输入窗-->
<script id="swagger_win" type="text/html">
    <div class="swagger_body">
        <textarea class="aui-input big_input" id="swagger_url"></textarea>
        <div class="aui-col-xs-12">
            <div class="aui-col-xs-9 aui-text-left">
                <div id="swagger-tips">
                   请输入swagger api的json地址
                </div>
            </div>
            <div class="aui-col-xs-3 aui-text-right">
                <button class="aui-btn" onclick="getSwaggerHistory();">历史记录</button>
                <button class="aui-btn aui-btn-primary" onclick="getSwaggerApi();">确定</button>
            </div>
        </div>
    </div>
</script>
<!--历史记录操作窗-->
<script id="history_win" type="text/html">

</script>
<!---api详情页--->
<script id="view_api_win" type="text/html">
    <div class="aui-col-xs-12">
        <div class="aui-col-xs-3">地址:</div>
        <div class="aui-col-xs-9">
            <input type="text" value="{{=it.url}}" id="api_url"/>
        </div>
    </div>
    <div class="aui-col-xs-12">
        <div class="aui-col-xs-3">请求参数:</div>
        <div class="aui-col-xs-9"><textarea id="api_request">{{=it.request||'{}'}}</textarea></div>
    </div>
    <div class="aui-col-xs-12">
        <div class="aui-col-xs-3">返回参数:</div>
        <div class="aui-col-xs-9"><textarea id="api_respone">{{=it.respone||'{}'}}</textarea></div>
    </div>
    <div class="aui-col-xs-12">
        <div class="aui-col-xs-3">备注:</div>
        <div class="aui-col-xs-9"><textarea id="api_remark">{{=it.remark||''}}</textarea></div>
    </div>
    <div class="aui-col-xs-12">
        <div class="aui-col-xs-8" id="view_tips"></div>
        <div class="aui-col-xs-4">
            <button class="aui-btn aui-btn-success" onclick="saveInfo();">保存</button>
        </div>
    </div>
</script>
<!--数据源选择列表-->
<script id="select_source" type="text/html">
    <input  type="text" id="searchSource" placeholder="请输入搜索内容"/>
    <input type="button" value="搜索" onclick="searchSource()"/>
    {{ for(var i in it) { }}
        <div class="layout printer" id="source_layout_{{=it[i].id}}">
            <div class="btn-group">
                <div class="aui-text-left"> {{=it[i].domain}}</div>
                <div class="aui-pull-right">
                <button class="aui-btn aui-btn-success" onclick="selectSource('{{=it[i].domainMd5}}','{{=it[i].domain}}')">选择</button>
                <button class="aui-btn aui-btn-danger" onclick="deleteSource('{{=it[i].domainMd5}}')">删除</button>
                </div>
            </div>
        </div>
    {{ } }}
</script>
<!--swagger历史记录-->
<script id="swagger_history" type="text/html">
    {{ for(var i in it) { }}
    <div class="layout printer" id="swagger_history_layout_{{=i}}">
        <div class="btn-group">
            <div class="aui-text-left">{{=it[i].value}}</div>
            <button class="aui-btn aui-btn-success" onclick="selectSwaggerUrl('{{=it[i].value}}')">选择</button>
            <button class="aui-btn aui-btn-danger" onclick="deleteSwaggerUrl('{{=it[i].id}}')">删除</button>
        </div>
    </div>
    {{ } }}
</script>

<script id="add_action" type="text/html">
    <div class="aui-form" style="text-align: left" id="add_action_form">
        <div>
            URL：<textarea id="addaction-url" class="aui-input"></textarea>
        </div>
        <div>
            method：<textarea  id="addaction-method"  class="aui-input "></textarea>
        </div>
        <div>
            request：<textarea  id="addaction-request"  class="aui-input "></textarea>
        </div>
        <div >
            respone：<textarea  id="addaction-respone"  class="aui-input"></textarea>
        </div>
        <div>
            备注：<textarea  id="addaction-remark"  class="aui-input" ></textarea>
        </div>
        <div>
            <button class="aui-btn aui-btn-primary" onclick="addAction()">添加</button>
        </div>
    </div>
</script>
<!--ROOT URL 管理窗-->
<script id="root_url_manager" type="text/html">

    <div class="aui-form" style="text-align: left" id="root_url_form">
        <div style="float: right" >
            <button class="aui-btn aui-btn-primary" onclick="addRootUrl('')">添加</button>
            <button class="aui-btn aui-btn-primary" onclick="autoAddRootUrl()">自动提取</button>
        </div>
        <div>
            KEY：<input id="rooturl-key" class="aui-input"/>
        </div>
        <div>
            生产环境ROOT：<input  id="rooturl-url"  class="aui-input "/>
        </div>
        <div >
            测试环境ROOT：<input  id="rooturl-testUrl"  class="aui-input"/>
        </div>
        <div>
            开发环境ROOT：<input  id="rooturl-devUrl"  class="aui-input" />
        </div>
        <div>
            备注：<textarea  id="rooturl-remark"  class="aui-input" ></textarea>
        </div>
    </div>

    {{ for(var i in it) { }}
    <div class="layout printer" id="root_url_{{=it[i].id}}">
        <div class="btn-group">
            <div class="aui-form" style="text-align: left" id="root_url_form{{=i}}">
                <input type="hidden" id="rooturl{{=i}}-id" value="{{=it[i].id}}"/>
                <div>
                    KEY：<input id="rooturl{{=i}}-key" value="{{=it[i].key}}" class="aui-input"/>
                </div>
                <div>
                    生产环境ROOT：<input  id="rooturl{{=i}}-url"   value="{{=it[i].url}}"  class="aui-input "/>
                </div>
                <div >
                    测试环境ROOT：<input  id="rooturl{{=i}}-testUrl"   value="{{=it[i].testUrl}}"    class="aui-input"/>
                </div>
                <div>
                    开发环境ROOT：<input  id="rooturl{{=i}}-devUrl"  value="{{=it[i].devUrl}}"   class="aui-input" />
                </div>
                <div>
                    备注：<textarea  id="rooturl{{=i}}-remark"  class="aui-input" >{{=it[i].remark}}</textarea>
                </div>
            </div>
            <div class="aui-pull-right">
                <button class="aui-btn aui-btn-success" onclick="addRootUrl('{{=i}}')">修改</button>
                <button class="aui-btn aui-btn-danger" onclick="deleteRootUrl('{{=it[i].id}}')">删除</button>
            </div>
        </div>
    </div>
    {{ } }}
</script>
 <script type="text/javascript">
     function toggleLayout(obj,a){
         var dom = $api.byId('api_dom_'+a);
         if($api.hasCls(dom,'hide')){
             $api.removeCls(dom,'hide');
             $api.html(obj,"收起");
         }else{
             $api.addCls(dom,'hide');
             $api.html(obj,"展开");
         }
     }
     var hasDel = false ;
     function hasDelList(){
         var btn = $api.byId('hasDelBtn');
         if(hasDel){
             $api.removeCls(btn,'aui-btn-warning');
             $api.addCls(btn,'aui-btn-default');
             hasDel = false;
         }else{
             $api.removeCls(btn,'aui-btn-default');
             $api.addCls(btn,'aui-btn-warning');
             hasDel = true;
         }
         getApiInfo();
     }
     //swagger历史记录
     function getSwaggerHistory(){
         if($api.disableWin){
             return;
         }
         $api.getStroageList('swagger_url',null,function(ret,err){
             if(ret){
                 $api.showWin('swagger_history','请选择历史记录',ret);
             }
         });
     }
     //删除swaggerurl记录
     function deleteSwaggerUrl(id){
         this.id = id;
         $api.rmStorage(id,function(ret,err){
             if(ret){
                 $api.remove($api.byId('swagger_history_layout_'+id));
             }else{

             }
         });
     }
     //修改别名
     function changeAlias(dom,id){
         var val = $api.val($api.byId(dom));
         $api.changeAlias(val,id,function(ret,err){
            if(ret){
                tips(ret);
            }else{
                tips(err);
            }
         });
     }
     //选择swaggerurl
     function selectSwaggerUrl(value){
         $api.val($api.byId('swagger_url'),value);
         $api.hideWin();
     }
     //删除数据源
     function deleteSource(id){
         $api.deleteSource(id,function(ret,err){
            if(ret){
                $api.remove($api.byId("source_layout_"+id));
                $api.hideWin();
                tips(ret);
                getApiInfo();
            }else{
                tips(err);
            }
         });
     }
     //选择数据源
     var selectDomain;
     function selectSource(id,name){
        this.selectDomain = id;
         getApiInfo();
         $api.hideWin();
         if(name.indexOf("-")!=-1){
             var n = name.split('-');
             name = n[n.length-1];
         }
         $api.html($api.byId("selectSource"),'数据源:'+name)
     }
     //弹出数据源选择窗
     function showSelectSource(){
         $api.getDomainInfo(function(ret,err){
             if(ret){
                 $api.showWin('select_source','请选择数据源',ret);
             }else{
                 tips("载入数据出错!")
             }
         });
     }
     function searchSource(){

     }
     //查看api详情
     var tempDetail;
     function viewApi(a,i){
         if(!apiInfos){
             return;
         }
         tempDetail= apiInfos[a].apiActions[i];
         var url = tempDetail.url.split("/");
         $api.showWin('view_api_win',tempDetail.title||url[url.length-1],tempDetail);
     }
     //保存api修改
     function saveInfo(){
         var id = tempDetail.id;
         var url = $api.val($api.byId("api_url"));
         var req =  $api.val($api.byId("api_request"));
         var res =  $api.val($api.byId("api_respone"));
         var remark =  $api.val($api.byId("api_remark"));
         $api.saveApi(id,url,req,res,remark,function(ret,err){
            if(ret){
                tips("保存成功!")
            } else{
                tips(err);
            }
         });
     }
     //备注api
     function remark(a,i){

     }
     //生成代码
     var id;
     function generateCode(id){
         this.id = id;
         $api.generateCode(id,function(ret,err){
             if(ret){
                 $api.html($api.byId("tips_"+id),ret+"<a href='javascript:openSourcePath();'>[打开位置]</a>");
             }else{
                 $api.html($api.byId("tips_"+id),err.message);
             }
         });
     }
     function generateCode1(id){
         this.id = id;
         $api.generateCode1(id,function(ret,err){
             if(ret){
                 $api.html($api.byId("tips_"+id),ret+"<a href='javascript:openSourcePath();'>[打开位置]</a>");
             }else{
                 $api.html($api.byId("tips_"+id),err.message);
             }
         });
     }

     function generateCode2(id){
         this.id = id;
         $api.generateCode2(id,function(ret,err){
             if(ret){
                 $api.html($api.byId("tips_"+id),ret+"<a href='javascript:openSourcePath();'>[打开位置]</a>");
             }else{
                 $api.html($api.byId("tips_"+id),err.message);
             }
         });
     }
     //删除整组api
     var gid,cid;
     function delApiByGroupId(gid,id){
         this.gid = gid;
         $api.delApiByGroupId(id,function(ret,err){
             if(ret){
                 $api.remove($api.byId("api_layout_"+gid));
             }else{
                 tips(JSON.stringify(err));
             }
         });
     }
     //隐藏或删除单条api
     function delApi(gid,cid,id){
         this.gid = gid;
         this.cid = cid;
         $api.delApi(id,function(ret,err){
            if(ret){
                $api.remove($api.byId("api_dom_"+gid+"_"+cid));
            }else{

            }
         });
     }
     function openSourcePath(){
         $api.openSourcePath();
     }
     //通过swagger解析api并保存到数据库
     function bySwagger(){
         $api.showWin('swagger_win','请输入swagger api 的json地址');
        $api.getStorage("swagger_url",function(ret,err){
             if(ret){
                 $api.val($api.byId("swagger_url"),ret);
             }
         });
     }
     //获取swagger json
     function getSwaggerApi(){
         var url = $api.val($api.byId("swagger_url"));
         url = url.replace(/\s+/g,"");
         if(!url){
             $api.showTips('swagger-tips','url地址不能为空!');
             return;
         }
        getData(url);
     }
     //获取swagger json
     function getData(url){
         if($api.disableWin){
             return;
         }
         $api.showTips('swagger-tips','正在加载数据...');
         $api.ajax(url,'get',{},'text',function(data,errcode){
             if(data){
                 $api.setStorage("swagger_url",url);
                 $api.showTips('swagger-tips','正在解析数据...');
                 $api.disableWin = true;
                 $api.call("analysisSwagger",data,function(res,err){
                     if(res){
                         $api.disableWin = false;
                         $api.showTips('swagger-tips',res);
                         setTimeout(function(){
                             $api.hideWin();
                             getApiInfo();
                         },2000);
                     }else{
                         if(err.code==0){
                             $api.disableWin = false;
                         }
                         $api.showTips('swagger-tips',err.message);
                     }
                 });
             }else{
                 $api.showTips('swagger-tips','网络请求失败,状态码:'+errcode);
             }

         });
     }
     //获取findler数据解析api并保存
     function byFindler(){
         $api.openFile(function (res,err) {
            if(res){
                tips("解析成功!")
                getApiInfo();
            } else {
                tips(err.message);
            }
         });
     }
     //一键生成全部代码
     function generateCodeAll(){
         $api.generateCodeAll(function(ret,err){
            if(ret){
                tips(ret+"<a href='javascript:openSourcePath();'>[打开位置]</a>");
            }else{
                tips(err.message);
            }
         });

     }
     var apiInfos ;
     var selectDomain;
     var searchVal;
     //加载api列表信息
     function getApiInfo(){
             $api.getApiInfo(selectDomain,searchVal,hasDel,function(res,err){
                 if(res){
                     apiInfos = res;
                     var html = $api.template("apiDom",res);
                     $api.html($api.byId("tab2_context"),html);
                 }else{
                     tips("加载数据失败");
                 }
             });
     }

     function searchList(){
         searchVal= $api.valById("searchval");
         getApiInfo();
     }


     function addActions(){
         $api.showWinOrReload('add_action','添加API');
     }
     function addAction(){
         var url = $api.valById('addaction-url');
         var method = $api.valById('addaction-method');
         var request = $api.valById('addaction-request');
         var respone = $api.valById('addaction-respone');
         var remark = $api.valById('addaction-remark');
         url+="@"+method;
         $api.saveApi('',url,request,respone,remark,function(ret,err){
             if(ret){
                 tips("保存成功!");
                 getApiInfo();
             }else{
                 tips(err);
             }
         });
     }

     //显示 root url 管理窗口
     function rootUrlManager(){
         $api.getRootUrl(function(ret,err){
             if(ret){
                 $api.showWinOrReload('root_url_manager','ROOT URL 管理',ret);
             }else{
                 tips("载入数据出错!");
             }
         });
     }
     //解析java代码批量更新
     function autoAddRootUrl(){
         var val = $api.valById("rooturl-remark");
         if(val){
             val = val.replace(";","\n");
            var s = val.split("\n");
            if(s.length>0){
                var arr = new Array();
                var remark ,key,url;
                for(var i = 0 ;i<s.length;i++){
                    var a = s[i].trim();
                    if(a.length<2){
                        continue;
                    }
                    if(a.length>2){
                        if(a.substring(0,2)=='//'){
                            remark = a.substring(2);
                            continue;
                        }
                        if(a.indexOf("=")>-1){
                            var m = a.split("=");
                            var left = m[0].trim();
                            var right = m[1].trim();
                            key = left.substring(left.lastIndexOf(" ")+1);
                            url = right.substring(right.indexOf('"')+1,right.lastIndexOf('"'));
                            if(url.length<=4||url.length>4&&url.substring(0,4)!='http'){
                                key=null;
                                url=null;
                                remark=null;
                                continue;
                            }
                        }
                        if(key!=null&&url!=null){
                            var data = {};
                            data.key = key;
                            data.url = url;
                            data.remark = remark;
                            data.devUrl = "";
                            data.testUrl="";
                            arr.push(data);
                            key=null;
                            url=null;
                            remark=null;
                        }
                    }
                }
                $api.addRootUrls(arr,function(ret,err){
                    if(ret){
                        tips(ret);
                        rootUrlManager();
                    }else{
                        tips(err.message);
                    }
                });
            }
         }else{
             tips("请在备注里粘贴java代码");
         }
     }
     //添加或修改root url
     function addRootUrl(id){
         var data = $api.getFormData("root_url_form"+id);
         $api.addRootUrl(data,function(ret,err){
             if(ret){
                 tips(ret);
                 rootUrlManager();
             }else{
                 tips(err.message);
             }
         });
     }
     //删除root url
     function deleteRootUrl(id){
         $api.deleteRootUrl(id,function(ret,err){
             if(ret){
                 tips(ret);
                 rootUrlManager();
             }else{
                 tips(err.message);
             }
         });
     }

     //载入完成执行
     $api.onReady(function(){
         getApiInfo();
     });
 </script>
</body>
</html>
